import Vue from 'vue';
import App from './App';
import { createPinia, PiniaVuePlugin } from 'pinia'

// 路由
import { router, RouterMount } from './router/index.js'
Vue.use(router)

// vue3 组合式api
import VueCompositionAPI from '@vue/composition-api'
Vue.use(VueCompositionAPI)
import EventBus from '@/utils/eventBus.js';
Vue.use(EventBus)

// 全局loading
import GlobalLoading from '@/components/commV2/GlobalLoading/index.vue'
// 创建 Loading 实例并挂载到 DOM
const LoadingConstructor = Vue.extend(GlobalLoading)
const loadingInstance = new LoadingConstructor().$mount()
document.body.appendChild(loadingInstance.$el)
// 挂载到 Vue 原型
Vue.prototype.$loadingGlobal = loadingInstance

import eventCtrl from "./js/event/EventCtrl.js";

import scanGunCtrl from "js/scanGun/ScanGunCtrl.js";

import store from './store';

import Net from 'js/https/Net.js';

import debug from "./js/debug/Debug.js";
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import BaiduMap from 'vue-baidu-map'
Vue.use(BaiduMap, {
	ak: 'aBqk5IAE1lOVYNmH8zPVZHEOqAQO4xEI'
})
Vue.use(ElementUI)

Vue.prototype.showRTBtns = false;

if (window.location.host == 'pc.imiyun.com' || window.location.host == 'pc.test.imiyun.com') {
	Vue.prototype.showRTBtns = false;
}
Vue.config.productionTip = false;

Vue.prototype.$store = store;

Vue.prototype.scanGunCtrl = scanGunCtrl;

Vue.prototype.scanGunCtrl.addEvent();

// 获取url参数
Vue.prototype.$geturl = function () {
	let url = window.location.hash;
	let theRequest = new Object();
	if (url.indexOf("?") != -1) {
		let str = url.split("?")[1];
		let strs = str.split("&");
		for (let i = 0; i < strs.length; i++) {
			theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
		}
	}
	return theRequest
}

Vue.prototype.$msg = ({
	title,
	callback = null,
	duration = 1500,
	mask = false,
	icon = 'none'
}) => {
	if (Boolean(title) === false) {
		return;
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon
	});
	if (callback) {
		setTimeout(callback, duration);
	}
}

let href = window.location.href;
if (href.indexOf("debug=1") == -1 && window.location.host == 'pc.imiyun.com' || href.indexOf("debug=1") == -1 && window.location.host == 'cdn.imiyun.com') //生成环境
{

	console.log = () => { };
}


// 全局注册组件
import { registerGlobalComponents } from '@/utils/registerComponents'
registerGlobalComponents() 


/****窗口尺寸发生变化******/
uni.onWindowResize(function (res) {
	uni.$emit(eventCtrl.CHANGE_SIZE_EVENT, res);
})

Vue.prototype.$api = Net.request;

Vue.prototype.$debug = debug;

Vue.prototype.$backgroundAudioData = {
	playing: false,
	playTime: 0,
	formatedPlayTime: '00:00:00'
}

// 滚动加载更多指令
Vue.directive('loadMore', {
	bind(el, binding) {
		// 获取element，定义scroll
		let select_dom = el.querySelector('.el-select-dropdown .el-select-dropdown__wrap');
		select_dom.addEventListener('scroll', function () {
			let height = this.scrollHeight - this.scrollTop <= this.clientHeight;
			if (height) {
				binding.value()
			}
		})
	}
});

App.mpType = 'app';

// Create and install Pinia
const pinia = createPinia()
Vue.use(PiniaVuePlugin)

const app = new (typeof App === 'function' ? App : Vue.extend(Object.assign({ 
	mpType: 'app',
	pinia, // Add pinia to the app instance
}, App, store)))

// 安装路由插件
// Vue.use(router)

// 使用 RouterMount 挂载路由
RouterMount(app, router, '#app')

app.$mount()